package com.platform.qujia.pay.tianquetechPay;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.platform.qujia.pojo.TradePaymentRecordsDO;
import com.platform.qujia.utils.ApiRequestBean;
import com.platform.qujia.utils.HttpUtils;
import com.platform.qujia.utils.RSASignature;
import com.platform.qujia.utils.sxfpay.SXFPayUtil;

//import cm.suixingpay.utils.ApiRequestBean;
//import cm.suixingpay.utils.HttpUtils;
//import cm.suixingpay.utils.RSASignature;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

/***
 * 暂时废弃的类
 * @author adminLiu
 *
 */
public class TradeReverseScanPay {

	
	private static Logger logger =(Logger) LoggerFactory.getLogger(SXFPayUtil.class);
	private  static String reverseScanURL = "/order/reverseScan";
	private  static String refundScanURL = "/order/refund";
	
//    @Value("${tianque.tianqueURL}")  
    private static String tianqueURL="https://openapi-test.tianquetech.com";
	
	// 支付、退款、查询
    //被扫支付
	public static void reverseScanPay() {
	        String ordNo = (int) (Math.random() * 1000000000) + "" + (int) (Math.random() * 1000000000);
	        try {
	            //合作方私钥(替换成自己的)
	            String privateKey = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCzsbkXTjrpxzSAlJK4oELORda7IfiSrZynohKVRzb8m+7PfvVw5INL6w3raAhWRM5AMo7jwUPGAJsxFHr5p8QGhCH4giJhnvvVLS3TT67dkODXVQ/tZ6pN+I3SKwKim/mvZKR/sIFdj8+hdfL3MjrEvn/qa4Fyc4tePl8xDWlxFXC0n+WffOEgAa0dhLq4PqszLvusQClJk1nSseuMPIS4BTJQgTrEDWalZ6MhopaAd+hAjhb//kG3Ym91QzUdTwyaqnF+RcwAv+jA0oiicG0ATSQV5EshWo9vileeeoxqWeAmIaFoK+sAX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2pBtGv5/L5YAzwPqcBmaAuM9oqECgYEAwhdwTLvxx5TWC1HaQhSP7K7wPX0/DFLzmwdHfKs2711mtnBiTUzuMyvMQ5hSY2ABJ+xBMe7Ts5sujj+hp4zMur/G9okQQxKdtBxx5HgAp7DW9TQaQNmso0MfefNkhkNB5AmML8ywAnzoTMLAZa9GtqW/TKTgUUkDQ1TXo23S8JA=";
	            
	            //随行付公钥
	            String sxfPublic =  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs7G5F0466cc0gJSSuKBC"
	            + "zkXWuyH4kq2cp6ISlUc2/Jvuz371cOSDS+sN62gIVkTOQDKO48FDxgCbMRR6+afE"
	            + "BoQh+IIiYZ771S0t00+u3ZDg11UP7WeqTfiN0isCopv5r2Skf7CBXY/PoXXy9zI6"
	            + "xL5/6muBcnOLXj5fMQ1pcRVwtJ/ln3zhIAGtHYS6uD6rMy77rEApSZNZ0rHrjDyE"
	            + "uAUyUIE6xA1mpWejIaKWgHfoQI4W//5Bt2JvdUM1HU8MmqpxfkXMAL/owNKIonBt"
	            + "AE0kFeRLIVqPb4pXnnqMalngJiGhaCvrAF64+4w9XVdfUTlo9Aa6x+zF670oGanv"
	            + "cQIDAQAB";
	            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	            String dataStr = df.format(new Date());

	            ApiRequestBean<JSONObject> reqBean = new ApiRequestBean<JSONObject>();

	            reqBean.setOrgId("57308998");
//	            reqBean.setReqId(UUID.randomUUID().toString().replaceAll("-", ""));
	            reqBean.setReqId("77fe20f79fc24b54adec1fba4e0d1ea1");
	            reqBean.setSignType("RSA");
	            reqBean.setTimestamp(dataStr);
	            reqBean.setVersion("1.0");
	            JSONObject reqData = new JSONObject();

	            //业务参数
	            reqData.put("mno", "399200503888644"); //商户编号
	            //reqData.put("subMechId", ""); //子商户号
	            //reqData.put("subAppid", ""); //微信 subAppId
	            reqData.put("ordNo", ordNo); //商户订单号
	            reqData.put("authCode", "131518436987228122"); //授权码
	            reqData.put("amt", "0.01"); //订单总金额
	            //reqData.put("discountAmt", ""); //参与优惠金额
	            //reqData.put("unDiscountAmt", ""); //不参与优惠金额
//	            reqData.put("payType", "WECHAT"); //支付渠道
//	            reqData.put("scene", "3"); //支付场景，1： 刷卡 2：声波 3：刷脸   不上传默认为 1
	            reqData.put("subject", "商品支付"); //订单标题
//	            reqData.put("tradeSource", "02"); //交易来源 01服务商，02收银台，03硬件
	            reqData.put("trmIp", "192.168.1.24");
	            reqBean.setReqData(reqData);
	            String req = JSONObject.toJSONString(reqBean);
	            //请求数据json字符串示例
	            //String req= "{"orgId":"26680846","reqData":{"ordNo":"145957903824647204","authCode":"28763400000664394","payType":"WECHAT","subject":"B扫C测试","amt":"0.01","tradeSource":"02","trmIp":"172.16.2.1","mno":"399190910000387","scene":"3"},"reqId":"1336c88a4efd40a7b4c4afb691e5ec43","signType":"RSA","timestamp":"2020-04-03 18:05:12","version":"1.0"}";
	            System.out.println("req:" + req);
	            //此处不要改变reqData里面值的顺序用LinkedHashMap
	            HashMap reqMap = JSON.parseObject(req, LinkedHashMap.class, Feature.OrderedField);
	            //组装加密串
	            String signContent = RSASignature.getOrderContent(reqMap);
	            System.out.println("拼接后的参数：" + signContent);
	            //sign
	            String sign = RSASignature.encryptBASE64(RSASignature.sign(signContent, privateKey));
	            System.out.println("============签名:" + sign);
	            reqMap.put("sign", sign);

	            String reqStr = JSON.toJSONString(reqMap);
	            System.out.println("请求参数:" + reqMap);
	            System.out.println("请求参数:" + reqStr);
//	            String url = "https://openapi-test.tianquetech.com/order/reverseScan";

	            String resultJson = HttpUtils.connectPostUrl(tianqueURL+reverseScanURL, reqStr);

	            System.out.println("返回信息：" + resultJson);
	            //不要对reqData排序 所以用LinkedHashMap
	            HashMap<String, Object> result = JSON.parseObject(resultJson, LinkedHashMap.class, Feature.OrderedField);
	            System.out.println("result="+result.toString());
	            System.out.println("----------------------------------------");
	            if ("0000".equals(result.get("code"))) {
	                //验签
	                String signResult = result.get("sign").toString();
	                result.remove("sign");

	                String resultStr = RSASignature.getOrderContent(result);
	                System.out.println(resultStr);
	                //sign
	                String resultSign = RSASignature.encryptBASE64(RSASignature.sign(signContent, privateKey));
	                System.out.println("resultSign:" + resultSign);
	                //组装加密串
//	                if (RSASignature.doCheck(resultStr, signResult, sxfPublic)) {
//	                    System.out.println("===================验签成功==============");
//	                }
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	
	public static void reverseScanPayRuand(JSONObject reqData) {
		String ordNo = (int) (Math.random() * 1000000000) + "" + (int) (Math.random() * 1000000000);
        try {
            //合作方私钥(替换成自己的)
            String privateKey = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCzsbkXTjrpxzSAlJK4oELORda7IfiSrZynohKVRzb8m+7PfvVw5INL6w3raAhWRM5AMo7jwUPGAJsxFHr5p8QGhCH4giJhnvvVLS3TT67dkODXVQ/tZ6pN+I3SKwKim/mvZKR/sIFdj8+hdfL3MjrEvn/qa4Fyc4tePl8xDWlxFXC0n+WffOEgAa0dhLq4PqszLvusQClJk1nSseuMPIS4BTJQgTrEDWalZ6MhopaAd+hAjhb//kG3Ym91QzUdTwyaqnF+RcwAv+jA0oiicG0ATSQV5EshWo9vileeeoxqWeAmIaFoK+sAXrj7jD1dV19ROWj0BrrH7MXrvSgZqe9xAgMBAAECggEBAKmIaG8jgJOlPNR+1/pWQREpdNDy2PlR0LWbRFsQaEFlwKaFSn1rKrCKoIRTj2IM4mZRDNy2LwH0+nYQs4EtFdVr/jzeUh+2BjR2voBZ8knZ8TdXaoJ6RoPvl6YMrBzMeYJPup854KNsFeFoTYWJH0cVz8tUCDn55yhQQjEeTTfBBDuSI0GYWbHpxqbQ9gP1xCSLAJLxTpi1wa/Qmr3I1fFEpgFbae6j5+b98r98CnM1CnV2CmR0qGNPJ9LJyXVUwUt6Vhu23nQyf2IDXpgAzHssu5kuW5/uGJ2UvpRGZb+lDbZf/wEKbb0RVBpgC0zH8J5538C5dhcl8ydVsg+dnX0CgYEA45XEpG+0bpfgToD16IXtwoeM3ztufI4v0TuxzoSgJFRor+8R/ZjdDAtx+fmjUnApmXEMaXs6HVXlScHBcOgZTAfpr0xbbUBUgplUL4lTMIDVtVk43btiOz7231aBRpAUe9ojUT1rX0RQgQWyEDcOS1m2PkWqV3aDhc7Lc7lueDcCgYEAyiE8URt2xSekdk3cPueyLUuAs8+alNivKcDlJjtnYa9BtPReWGH+tIJC1D8zSguhIuRjZFlPJ2hTj7XLQY0lMlgWEDG4k/VmNGXgcjuAhwUFFyDS+OnWD7OyzIlahDOVKnvbUD/pWG8acsmHj9KEfnmMp8dAaJC+00731bM1sZcCgYEAzvxG1Vs3GiKNgjit7qXoQTVxPRJbgXL8MGr/YxmiYIhYkjJ4BVKDMFI0fiq6jN6wJvr3U/QcPdQCbpuG6L5fS5QiyClFOc75TbJQK8/U1ldGhuI34wpq5AF3HFcbymZ+CsqxtlCLGCEUqtPYSAEawhM8udQ4gcWrBwa2aXDr9O8CgYEArBL/Nk0bH1xQNiayiRrWJJlSR8FE59JPzEvayGFp8U8xsMsYRkWmgCUkn2vuTer4kqu7sYIWUIYQHRe2oWd3cxRqM/8gE1WmXJUeLiFZmZg8bSQUdrMa+OMSI+4KkOEY68HlQrwl+pMbAP1v2pBtGv5/L5YAzwPqcBmaAuM9oqECgYEAwhdwTLvxx5TWC1HaQhSP7K7wPX0/DFLzmwdHfKs2711mtnBiTUzuMyvMQ5hSY2ABJ+xBMe7Ts5sujj+hp4zMur/G9okQQxKdtBxx5HgAp7DW9TQaQNmso0MfefNkhkNB5AmML8ywAnzoTMLAZa9GtqW/TKTgUUkDQ1TXo23S8JA=";
            
            //随行付公钥
            String sxfPublic =  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs7G5F0466cc0gJSSuKBC"
            + "zkXWuyH4kq2cp6ISlUc2/Jvuz371cOSDS+sN62gIVkTOQDKO48FDxgCbMRR6+afE"
            + "BoQh+IIiYZ771S0t00+u3ZDg11UP7WeqTfiN0isCopv5r2Skf7CBXY/PoXXy9zI6"
            + "xL5/6muBcnOLXj5fMQ1pcRVwtJ/ln3zhIAGtHYS6uD6rMy77rEApSZNZ0rHrjDyE"
            + "uAUyUIE6xA1mpWejIaKWgHfoQI4W//5Bt2JvdUM1HU8MmqpxfkXMAL/owNKIonBt"
            + "AE0kFeRLIVqPb4pXnnqMalngJiGhaCvrAF64+4w9XVdfUTlo9Aa6x+zF670oGanv"
            + "cQIDAQAB";
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String dataStr = df.format(new Date());

            ApiRequestBean<JSONObject> reqBean = new ApiRequestBean<JSONObject>();

            reqBean.setOrgId("57308998");
            reqBean.setReqId("77fe20f79fc24b54adec1fba4e0d1ea1");
            reqBean.setSignType("RSA");
            reqBean.setTimestamp(dataStr);
            reqBean.setVersion("1.0");

            reqBean.setReqData(reqData);
            String req = JSONObject.toJSONString(reqBean);
            //请求数据json字符串示例
            //String req= "{"orgId":"26680846","reqData":{"ordNo":"145957903824647204","authCode":"28763400000664394","payType":"WECHAT","subject":"B扫C测试","amt":"0.01","tradeSource":"02","trmIp":"172.16.2.1","mno":"399190910000387","scene":"3"},"reqId":"1336c88a4efd40a7b4c4afb691e5ec43","signType":"RSA","timestamp":"2020-04-03 18:05:12","version":"1.0"}";
            System.out.println("req:" + req);
            //此处不要改变reqData里面值的顺序用LinkedHashMap
            HashMap reqMap = JSON.parseObject(req, LinkedHashMap.class, Feature.OrderedField);
            //组装加密串
            String signContent = RSASignature.getOrderContent(reqMap);
            System.out.println("拼接后的参数：" + signContent);
            //sign
            String sign = RSASignature.encryptBASE64(RSASignature.sign(signContent, privateKey));
            System.out.println("============签名:" + sign);
            reqMap.put("sign", sign);

            String reqStr = JSON.toJSONString(reqMap);
            System.out.println("请求参数:" + reqMap);
            System.out.println("请求参数:" + reqStr);
//            String url = "https://openapi-test.tianquetech.com/order/reverseScan";

            String resultJson = HttpUtils.connectPostUrl(tianqueURL+refundScanURL, reqStr);

            System.out.println("返回信息：" + resultJson);
            //不要对reqData排序 所以用LinkedHashMap
            HashMap<String, Object> result = JSON.parseObject(resultJson, LinkedHashMap.class, Feature.OrderedField);
            System.out.println("result="+result.toString());
            System.out.println("----------------------------------------");
            if ("0000".equals(result.get("code"))) {
                //验签
                String signResult = result.get("sign").toString();
                result.remove("sign");

                String resultStr = RSASignature.getOrderContent(result);
                System.out.println(resultStr);
                //sign
                String resultSign = RSASignature.encryptBASE64(RSASignature.sign(signContent, privateKey));
                System.out.println("resultSign:" + resultSign);
                //组装加密串
//                if (RSASignature.doCheck(resultStr, signResult, sxfPublic)) {
//                    System.out.println("===================验签成功==============");
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	
}
